package top.readhub.wopei.client;

import com.fasterxml.jackson.core.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.readhub.wopei.client.helper.InstHelper;
import top.readhub.wopei.common.exception.WoPeiException;
import top.readhub.wopei.common.utils.CollectionUtil;
import top.readhub.wopei.common.utils.JsonUtil;
import top.readhub.wopei.common.utils.StringUtil;

import java.util.List;

/**
 * Created by daervin on 2021/6/9
 */
public class WoPei {
    private static final Logger logger = LoggerFactory.getLogger(WoPei.class);

    private WoPei() {
    }

    public static String get(String code) {
        try {
            return InstHelper.getLocalLoader().getSetting(code);
        } catch (WoPeiException e) {
            logger.error("WoPei#get {}-{} {}", e.getCode().code, e.getCode().msg, e.getCause());
        } catch (Exception e) {
            logger.error("WoPei#get {}", e.getCause());
        }
        return null;
    }

    public static <T> T get(String code, Class<T> tClass) {
        try {
            String config = InstHelper.getLocalLoader().getSetting(code);
            if (StringUtil.isBlank(config)) {
                return null;
            }
            return JsonUtil.toObject(config, tClass);
        } catch (WoPeiException e) {
            logger.error("WoPei#get {}-{} {}", e.getCode().code, e.getCode().msg, e.getCause());
        } catch (Exception e) {
            logger.error("WoPei#get {}", e.getCause());
        }
        return null;
    }

    public static String getByRemote(String code) {
        try {
            return InstHelper.getServiceLoader().getSetting(code);
        } catch (WoPeiException e) {
            logger.error("WoPei#getByRemote {}-{} {}", e.getCode().code, e.getCode().msg, e.getMessage());
        } catch (Exception e) {
            logger.error("WoPei#getByRemote {}", e.getMessage());
        }
        return null;
    }

    public static <T> T getByRemote(String code, Class<T> tClass) {
        try {
            String config = InstHelper.getServiceLoader().getSetting(code);
            if (StringUtil.isBlank(config)) {
                return null;
            }
            return JsonUtil.toObject(config, tClass);
        } catch (WoPeiException e) {
            logger.error("WoPei#getByRemote {}-{} {}", e.getCode().code, e.getCode().msg, e.getCause());
        } catch (Exception e) {
            logger.error("WoPei#getByRemote {}", e.getCause());
        }
        return null;
    }

    public static List<String> getAllByRemote(String code) {
        try {
            return InstHelper.getServiceLoader().getSettingAll(code);
        } catch (WoPeiException e) {
            logger.error("WoPei#getAllByRemote {}-{} {}", e.getCode().code, e.getCode().msg, e.getCause());
        } catch (Exception e) {
            logger.error("WoPei#getAllByRemote {}", e.getCause());
        }
        return null;
    }

    public static <T> List<T> getAllByRemote(String code, TypeReference<List<T>> jsonTypeReference) {
        try {
            List<String> configs = InstHelper.getServiceLoader().getSettingAll(code);
            if (CollectionUtil.isEmpty(configs)) {
                return null;
            }
            return JsonUtil.toObject(configs.toString(), jsonTypeReference);
        } catch (WoPeiException e) {
            logger.error("WoPei#getAllByRemote {}-{} {}", e.getCode().code, e.getCode().msg, e.getCause());
        } catch (Exception e) {
            logger.error("WoPei#getAllByRemote {}", e.getCause());
        }
        return null;
    }
}
